package de.fhb.itunes.repository.DataAccess;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import de.fhb.itunes.repository.data.Film_VO;
import de.fhb.itunes.repository.data.MedienFile_VO;
import de.fhb.itunes.repository.jdbc.DataAccessException;
import de.fhb.itunes.repository.jdbc.JdbcDAOBase;

public class DAO_MediaFile extends JdbcDAOBase implements IDAO_MediaFile{

	@Override
	public MedienFile_VO getByID(int id) {
		MedienFile_VO vo;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		String sqlStr = "SELECT * FROM  [MediaFileSatz] media WHERE media.id =  ?";

		try {
			con = getConnection();
			ps = con.prepareStatement(sqlStr);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			vo = (MedienFile_VO) transformSetToVO(rs);
		} catch (SQLException sqle) {
			throw new DataAccessException("Error executing " + sqlStr, sqle);
		} finally {
			try {
				// close the resources
				if (ps != null)
					ps.close();
				if (con != null)
					con.close();
			} catch (SQLException sqlee) {
				throw new DataAccessException("Can't close connection", sqlee);
			}
		}
		return vo;
	}

	@Override
	protected Object map2VO(ResultSet rs) throws SQLException {
		MedienFile_VO media = new MedienFile_VO();
		media.setAdded(rs.getDate("Added"));
		media.setGenre(rs.getString("Genre"));
		media.setId(rs.getInt("ID"));
		media.setLaenge(rs.getLong("Länge"));
		media.setPfad(rs.getString("Pfad"));
		media.setTrackid(rs.getInt("TrackID"));
		media.setTitel(rs.getString("Titel"));
		return media;
	}

}
